import type {
  SearchBaseSelectors,
  SearchPageSelectors,
} from '../../../../module/page_enhancer/appearance/style_enhancement/platforms'
import { AppStyleCollections } from '../../../../types/constants'

/**
 * 页面类型
 */
export enum GooglePageType {
  Index = 'index',
  IndexHk = 'index-hk',
  All = 'all',
  Video = 'video',
  News = 'news',
  ShortVideo = 'short-video',
  Web = 'web',
  Books = 'books',
  Advanced = 'advanced',
  NotFound = 'not-found',
  Undefined = 'undefined',
  // 暂不支持
  Photo = 'photo',
  Shopping = 'shopping',
  Flight = 'flight',
  Finance = 'finance',
}

/**
 * 页面类型属性
 */
export interface GooglePageTypeResult {
  type: GooglePageType | 'default'
}

// ====================================================== 基础配置 ======================================================
// 搜索框容器
export const GOOGLE_SEARCH_BOX_SELECTOR: string = '#sogou_wrap_id #searchForm'
// 搜索结果项
export const GOOGLE_SEARCH_RESULT_CONTAINER_SELECTOR: string = '#main #search #rso:not(:has(#kp-wp-tab-overview)), #main #search #rso #kp-wp-tab-overview'
export const GOOGLE_SEARCH_RESULT_ITEM_SELECTOR: string = '#main #search #rso:not(:has(#kp-wp-tab-overview)) > div:has(div[data-rpos]), #main #search #rso #kp-wp-tab-overview > div:not(:empty, :has(#tadsb))'
// 广告结果项
export const GOOGLE_SEARCH_AD_RESULT_CONTAINER_SELECTOR: string = ''
// 搜索框下拉菜单容器
export const GOOGLE_SEARCH_DROPDOWN_CONTAINER_SELECTOR: string = 'form[role=search] div[role=presentation] > ul[role=listbox]'
// 顶部菜单-用户中心
export const GOOGLE_HEADER_USER_CENTER_SELECTOR: string = '#searchform [role=search] + div:has(header)'
// 基础选择器
export const GOOGLE_SEARCH_BASE_SELECTORS: SearchBaseSelectors = {
  searchBox: {
    input: 'form[role=search] textarea[role="combobox"]',
    form: 'form[role=search]',
    dropdown: {
      wrapper: GOOGLE_SEARCH_BOX_SELECTOR,
      menu: ``,
      hotRank: ``,
      suggestions: `${GOOGLE_SEARCH_DROPDOWN_CONTAINER_SELECTOR} > li:has(div[role=button]:not([jsaction], [tabindex]), div[role=button][jsaction][jsaction=""])`,
      history: `${GOOGLE_SEARCH_DROPDOWN_CONTAINER_SELECTOR} > li:has(div[role=button][jsaction][jsaction^=click])`,
      clearHistoryBtn: `${GOOGLE_SEARCH_DROPDOWN_CONTAINER_SELECTOR} > li div[role=button][jsaction]:has(div[role=presentation]:contains(删除))`,
      clearHistoryText: '删除',
    },
  },
  result: {
    container: GOOGLE_SEARCH_RESULT_CONTAINER_SELECTOR,
    item: GOOGLE_SEARCH_RESULT_ITEM_SELECTOR,
  },
  adResult: {
    container: GOOGLE_SEARCH_AD_RESULT_CONTAINER_SELECTOR,
    item: `${GOOGLE_SEARCH_AD_RESULT_CONTAINER_SELECTOR}`,
    itemTitle: `${GOOGLE_SEARCH_AD_RESULT_CONTAINER_SELECTOR}`,
  },
  rightAside: {
    containerSelector: '',
    dragTargetSelector: `.${AppStyleCollections.Drag.AREA}`,
    dragTargetStyle: `bottom: -18px; left: -32px;`,
    handleIconStyle: ``,
    position: 'left',
  },
}

// 页面选择器
export const GOOGLE_VIDEO_PAGE_SEARCH_TIP_SELECTOR: string = '#main #search #rso > div:not(:has(h3))'
export const GOOGLE_VIDEO_DYNAMIC_RESULT_WRAPPER_SELECTOR: string = '#main #botstuff div[jsname] > div[id^=arc-srp]'
export const GOOGLE_VIDEO_DYNAMIC_RESULT_CONTAINER_SELECTOR: string = '#main #botstuff div[jsname]:has(> div[id^=arc-srp] > div[data-state-token])'
export const GOOGLE_VIDEO_RESULT_LOADING_CONTAINER_SELECTOR: string = '#main #botstuff div:has(> div[role="progressbar"])'
export const GOOGLE_VIDEO_RESULT_LOADING_SELECTOR: string = '#main #botstuff div[role="progressbar"]'
// 【视频】【元数据】比实际的结果项少了一个 div，主要是为了解决视频页面存在背景颜色层和内容层不在一个层级导致的样式问题
export const GOOGLE_VIDEO_PAGE_SEARCH_RESULT_META_SELECTOR: string = '#main :is(#search #rso > div:has(h3) > div, #botstuff div[data-state-token] > div:has(h3) > div)'
export const GOOGLE_SEARCH_PAGE_SELECTORS: SearchPageSelectors = {
  [GooglePageType.Index]: {
    ...GOOGLE_SEARCH_BASE_SELECTORS,
  },
  [GooglePageType.IndexHk]: {
    ...GOOGLE_SEARCH_BASE_SELECTORS,
  },
  [GooglePageType.All]: {
    ...GOOGLE_SEARCH_BASE_SELECTORS,
  },
  [GooglePageType.Video]: {
    ...GOOGLE_SEARCH_BASE_SELECTORS,
    result: {
      container: '#main :is(#search #rso > div:has(h3), #botstuff div[data-state-token] > div:has(h3))',
      item: `#main :is(#search #rso > div:has(h3) > div > div[jsname], #botstuff div[data-state-token] > div:has(h3) > div)`,
    },
  },
  [GooglePageType.Web]: {
    ...GOOGLE_SEARCH_BASE_SELECTORS,
    result: {
      container: '#main #search #rso',
      item: `#main #search #rso > div > div > div`,
    },
  },
  [GooglePageType.News]: {
    ...GOOGLE_SEARCH_BASE_SELECTORS,
    result: {
      container: '#main #search #rso > div > div',
      item: `#main #search #rso > div > div > div`,
    },
  },
  [GooglePageType.ShortVideo]: {
    ...GOOGLE_SEARCH_BASE_SELECTORS,
  },
  [GooglePageType.Books]: {
    ...GOOGLE_SEARCH_BASE_SELECTORS,
    result: {
      container: '#main #search #rso',
      item: `#main #search #rso > div > div > div`,
    },
  },
}
// 常用选择器
export const GOOGLE_SEARCH_SELECTORS: Record<'index', Record<'input' | 'suggestionContainer' | 'suggestionItem', string>> = {
  index: {
    input: GOOGLE_SEARCH_PAGE_SELECTORS[GooglePageType.Index]!.searchBox.input,
    suggestionContainer: 'form[role=search] div[role=presentation]',
    suggestionItem: `${GOOGLE_SEARCH_DROPDOWN_CONTAINER_SELECTOR} > li div[role=option] > div[role=presentation] > span`,
  },
}
